package com.gxa.adput.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.adput.mapper.AdPutMapper;
import com.gxa.adput.service.AdPutService;
import com.gxa.common.R;
import com.gxa.common.entity.platform_maintenance_service1001.AdPut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdPutServiceImpl implements AdPutService {

    @Autowired
    private AdPutMapper adPutMapper;
    @Override
    public R addAdPut(AdPut adPut) {
        R r = new R();

        int result = adPutMapper.insert(adPut);
        if(result > 0 ){
            r.setCode(200);
            r.setMessage("添加成功");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMessage("添加失败");
        }

        return r;
    }

    @Override
    public R updateAdPut(AdPut adPut) {
        R r = new R();
        AdPut selectById = adPutMapper.selectById(adPut.getId());
        if(selectById.getStatus() == 1){
            selectById.setStatus(0);int result = adPutMapper.updateAdPutById(adPut);
            r.setCode(200);
            r.setData(result);
            r.setMessage("修改成功");
            r.setData(selectById);
        }else {
            r.setCode(200);
            r.setMessage("已经发布，不需要发布");
            r.setData(selectById);
        }
        return r;
    }

    @Override
    public R selectAdPut(Integer currentPage, Integer pageSize) {
        R r = new R();
        QueryWrapper<AdPut> queryWrapper = new QueryWrapper<>();
        Page<AdPut> page = new Page<>(currentPage,pageSize);
        Page<AdPut> adPutPage = adPutMapper.selectPage(page, queryWrapper);
        r.setCode(200);
        r.setMessage("按照分页查询成功");
        r.setData(adPutPage);
        return r;
    }



    @Override
    public R delAdPutById(AdPut adPut) {
        R r = new R();
        long status = adPut.getStatus();
        if(status == 2){
            int result = adPutMapper.deleteById(adPut);
            if(result > 0){
                r.setCode(200);
                r.setMessage("删除成功");
                r.setData(result);
            }else {
                r.setCode(500);
                r.setMessage("删除失败");
            }
        }else if(status == 0){
            //根据id查询需要重新发布的内容
            AdPut selectAdPut = adPutMapper.selectById(adPut.getId());
            r.setCode(200);
            r.setMessage("重新发布成功");
            r.setData(selectAdPut);

        }else {
            r.setMessage("不需要删除");
        }
        return r;
    }

    @Override
    public R selectAdPutByCondition(AdPut adPut) {

        R r = new R();
        List<AdPut> adPutList = adPutMapper.selectAdPutByCondition(adPut);
        r.setCode(200);
        r.setMessage("条件查询成功");
        r.setData(adPutList);
         return r;
    }

    @Override
    public R selectAdPutAll() {
        R r = new R();
        List<AdPut> adPutList = adPutMapper.selectList(null);
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(adPutList);
        return r;
    }

//    @Scheduled(cron ="*/5 * * * * *" )
//    @Override
//    public R timedPush(AdPut adPut) {
//
//
//
//        return null;
//    }


}
